<?php

namespace app\api\controller;

use app\services\CouponService;
use app\services\GoodsActivityService;
use app\services\GoodsCommentService;
use app\services\GoodsService;
use app\services\OrderService;
use app\services\UserCollectService;
use app\services\UserService;
use app\traits\Page;
use plugin\admin\app\model\BrandCustomized;
use plugin\admin\app\model\Goods;
use plugin\admin\app\model\GoodsActivity;
use plugin\admin\app\model\UserCollect;
use support\Request;

class GoodsController
{
    public function list(Request $request)
    {
        try {
            $categoryId = $request->post('category_id', 0);
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword', '');
            $orderWay = $request->post('order_way', 1);
            $isIntegral = $request->post('is_integral', null);
            $isRecommend = $request->post('is_recommend', null);

            switch ($orderWay) {
                default:
                case 1:
                    $orderWay = ['id', 'desc'];
                    break;
                case 2:
                    $orderWay = ['price', 'desc'];
                    break;
                case 3:
                    $orderWay = ['price', 'asc'];
                    break;
                case 4:
                    $orderWay = ['created_time', 'desc'];
                    break;
                case 5:
                    $orderWay = ['sale_count', 'asc'];
                    break;
                case 6:
                    $orderWay = ['sale_count', 'desc'];
                    break;
            }

            return ok_rsp('成功', GoodsService::list($page, $length, $orderWay, $categoryId, $keyword, [], false, $isIntegral, $isRecommend));
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function guessYouLikeList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            return ok_rsp('成功', GoodsService::guessYouLikeList($page, $length));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function activityGoodsList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword', '');
            $orderWay = $request->post('order_way', 1);

            switch ($orderWay) {
                default:
                case 1:
                    $orderWay = ['id', 'desc'];
                    break;
                case 2:
                    $orderWay = ['price', 'desc'];
                    break;
                case 3:
                    $orderWay = ['price', 'asc'];
                    break;
                case 4:
                    $orderWay = ['created_time', 'desc'];
                    break;
            }

            $activity = GoodsActivityService::valid();

            if (!$activity) {
                return ok_rsp('成功', Page::wrap(0, $page, $length, []));
            }

            return ok_rsp('成功', GoodsActivityService::goodsList($activity['id'], $page, 10, $orderWay, $keyword));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }


    public function detail(Request $request)
    {
        try {
            $goodsId = $request->post('id', 0);
            return ok_rsp('成功', GoodsService::detail($goodsId));
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return e_rsp($e);
        }
    }

    public function coupons(Request $request)
    {
        try {
            $goodsId = $request->post('goods_id', 0);

            return ok_rsp('成功', CouponService::goodsCoupons($goodsId));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function collect(Request $request)
    {
        try {
            $goodsId = $request->post('id', 0);

            if (! Goods::find($goodsId)) {
                throw new \Exception('不存在的产品');
            }

            UserCollectService::collect(UserCollect::GOODS, $goodsId);
            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function uncollect(Request $request)
    {
        try {
            $goodsId = $request->post('id', 0);

            UserCollectService::uncollect(UserCollect::GOODS, $goodsId);
            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function commentCount(Request $request)
    {
        try {
            $goodsId = $request->post('goods_id', 0);

            $data = GoodsCommentService::getCommentCount($goodsId);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function commentList(Request $request)
    {
        try {
            $goodsId = $request->post('goods_id', 0);
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $filterType = $request->post('filter_type', 0);

            $data = GoodsCommentService::commentList($goodsId, $page, $length, $filterType);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}